package com.jwdf.uda.view.banner;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;


public class Banner extends LinearLayout {
    private AutoFlingPager mPager;
    private AutoFlingPagerAdapter<?> mPagerAdapter;
    private GalleryIndicator mPagerIndicator;
    private RelativeLayout mIndicatorLayout;

    public enum Rule {
        LEFT, RIGHT, CENTER
    }

    public Banner(Context context) {
        super(context);
        initViews(context);
    }

    public Banner(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    @Override
    public void setOnClickListener(OnClickListener clickListener) {
        mPager.setOnClickListener(clickListener);
    }

    public void setAdapter(AutoFlingPagerAdapter<?> adapter) {
        mPagerAdapter = adapter;
        mPager.setAdapter(mPagerAdapter);
        mPagerAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                mPagerIndicator.setCount(mPagerAdapter.getRealCount());
            }
        });
    }

    public void setDuration(int duration) {
        mPager.setDuration(duration);
    }

    public void stop() {
        mPager.stop();
    }

    public void start() {
        mPager.start();
    }

    private void initViews(Context context) {
        LayoutInflater.from(getContext()).inflate(R.layout.view_ad_banner, this, true);
        mPager = (AutoFlingPager) findViewById(R.id.pager_adbanner);
        mPager.setOnPageChangeListener(createOnPagerChangeListener());
        mPager.setDuration(1000);
        mPagerIndicator = (GalleryIndicator) findViewById(R.id.indicator);
        mIndicatorLayout = (RelativeLayout) findViewById(R.id.indicator_layout);
    }

    private OnPageChangeListener createOnPagerChangeListener() {
        return new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (mPagerAdapter != null) {
                    int currentPosition = position % mPagerAdapter.getRealCount();
                    mPagerIndicator.setSeletion(currentPosition);
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (android.os.Build.VERSION.SDK_INT <= 10) // 2.3版本及以下需一直调用此方法以解决无法滑动的问题
                {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
            }

            // 滑动时候，父控件不拦截事件！解决banner 不好滑动的问题。
            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
            }
        };
    }
}
